package com.robcity.platform.application;

import cn.hutool.json.JSONUtil;
import com.robcity.platform.dto.RobotLocationInfoSaveDTO;
import com.robcity.platform.param.RobotReqCommon;
import com.robcity.platform.param.RobotUploadReq;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.math.BigDecimal;

public class GzhClient {
    public static void main(String[] args) {
        EventLoopGroup eventExecutors = null;
        ChannelFuture channelFuture = null;
        try{
//            while (true) {
            eventExecutors = new NioEventLoopGroup();
//            Scanner scanner = new Scanner(System.in);
            RobotReqCommon robotReqCommon=new RobotReqCommon();
            robotReqCommon.setMsgType(1);
            RobotLocationInfoSaveDTO robotLocationInfoDTO = new RobotLocationInfoSaveDTO();
            robotLocationInfoDTO.setRob_id("123456");
            robotLocationInfoDTO.setTimeStemp("12345");
            robotLocationInfoDTO.setAlt(BigDecimal.valueOf(123.123));
            robotLocationInfoDTO.setCurCmd("测试");
            robotLocationInfoDTO.setLat(BigDecimal.valueOf(123.123));
            robotLocationInfoDTO.setLon(BigDecimal.valueOf(123.123));
            robotLocationInfoDTO.setBatLife("10");
            RobotUploadReq req = new RobotUploadReq();
            req.setCommon(robotReqCommon);
            req.setData(robotLocationInfoDTO);
            String json =  JSONUtil.toJsonStr(req);
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(eventExecutors).channel(NioSocketChannel.class)
                    .handler(new GzhClientInitializer(json));
            System.out.println("启动客户端");
            channelFuture = bootstrap.connect("localhost", 8090).sync();
//            }
//            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            eventExecutors.shutdownGracefully();
        }
    }
}
